<template>
  <div class="pic">
      <div
        style="
          height: auto;
          margin: 0 auto;
          padding: 20px;        "
      >

      <div style="font-size:28px;line-height:3.0;font-family:Helvetica Neue;text-align:center;color:#FFFF37">授权绑定(家长)</div>
        <el-form
          label-position="right"
          label-width="0"
          :model="form"
          ref="form"
          :rules="rules"
        >
          <el-form-item prop="tenantId">
            <el-input
              v-model="form.tenantId"
              placeholder="学校编号"
            ></el-input>
          </el-form-item>
          <el-form-item prop="studentName">
            <el-input
              v-model="form.studentName"
              placeholder="学生姓名"
            ></el-input>
          </el-form-item>
          <el-form-item prop="parentPhone">
            <el-input
              v-model="form.parentPhone"
              placeholder="监护人手机号码"
            ></el-input>
          </el-form-item>
        </el-form>

        <el-button type="primary" @click="submitForm" style="width:100%">提 交 绑 定</el-button>

        <div style="line-height:2.0;font-family:Helvetica Neue">
          <div v-if="msg == null && tip == null">
            <span style="color:red;margin-right:3px">*说明:</span>
            <span style="color:darkgrey;margin-right:3px">请输入预留在学校的监护人手机号，学校编号如有疑问，请咨询客服！</span>
          </div>
          <div v-if="msg != null">
            <span style="color:red;margin-right:3px">*提示:</span>
            <span style="color:darkgrey;margin-right:3px">{{msg}}</span>
          </div>
          <div v-if="tip != null">
            <span style="color:red;margin-right:3px">*提示:</span>
            <span style="color:darkgrey;margin-right:3px">{{tip}}<a href="https://zhxypt.net/wechat/code">https://zhxypt.net/wechat/code</a></span>
          </div>
        </div>



      </div>
  </div>
</template>

<script>
import {
  getWechatOfficialOpenId,
  saveOfficialOpenId,
} from "@/api/wechat/wechat.js";

export default {
  data() {
    return {
      code: null,
      msg: null,
      tip:null,
      form: {
        tenantId:"",
        studentName: "",
        parentPhone: "",
        openId: "",
      },
      rules: {
        tenantId: [{ required: true, message: "必填", trigger: "blur" }],
        studentName: [{ required: true, message: "必填", trigger: "blur" }],
        parentPhone: [{ required: true, message: "必填", trigger: "blur" }],
      },
    };
  },

  mounted() {
    this.code = new URLSearchParams(window.location.search).get("code");
    this.getOpenId();
  },
  methods: {
    getOpenId() {
      if (this.code) {
        let obj = { code: this.code };
        getWechatOfficialOpenId(obj).then((resp) => {
          this.form.openId = resp.data;
        });
      }
    },

    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          saveOfficialOpenId(this.form).then((resp) => {
            this.msg = null;
            this.tip = null;
            if (resp.code == 200) {
              if(resp.data == 1){
                // this.$message({ message: "绑定成功", type: "success" });
                let orgId = this.form.tenantId;
                if("1"!= orgId){
                  let outOpenId = this.form.openId;
                  let url = `https://smsh.shomr.com/wx/#/phoneMajorWxAgentJumpThirdIndex?agentShowCut=SGZhiNeng&org_id=${orgId}&outOpenId=${outOpenId}`;
                  window.location.href = url;
                }else{
                  this.$message({ message: "绑定成功", type: "success" });
                }
              }else if(resp.data == -1){
                this.msg = "未能查询到该学生，请核对填写的信息是否为所预留的信息！也可更换监护人姓名和手机号码进行再次提交尝试！！！"
              }else if(resp.data == -2){
                this.tip = "参数丢失，请点击链接重新获取参数，"
              }
            }
          });
        }
      });
    },
  },
};
</script>

<style lang="scss"  scoped>
 .pic {
  background-image: url("~@/assets/bg-m.jpg");
  height: 100%;
  width: auto;
  background-size: cover;
  background-repeat: no-repeat;
}
</style>
